package cn.itsource.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.api.openfeign.ILoginOpenFeignClient;
import cn.itsource.constants.ErrorCode;
import cn.itsource.constants.RConst;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.dto.RegisterDto;
import cn.itsource.pojo.ro.LoginRo;
import cn.itsource.pojo.ro.WxChatRegisterRo;
import cn.itsource.result.JSONResult;
import cn.itsource.service.ICustomerService;
import cn.itsource.service.WeChatService;
import cn.itsource.utils.BitStatesUtils;
import cn.itsource.utils.NameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author zcj
 * @since 2024-07-08
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private ILoginOpenFeignClient loginOpenFeignClient;

    @Override
    public JSONResult wxChatRegister(WxChatRegisterRo registerRo) {
        RegisterDto registerDto = weChatService.getWeChatOpenId(registerRo.getCode());
//        String userPhone = weChatService.getUserPhone(registerRo.getPhoneCode());
        String userPhone = "18723001111";
        //先判断 如果已经存在那么久不保存
        Customer one = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, registerDto.getOpenid()));
        if(one == null){
            String name = NameUtil.getName();
            initCustomer(registerDto,userPhone,name);
            loginOpenFeignClient.createLogin(buildLoginRo(registerDto,name,userPhone));
            return JSONResult.message("微信绑定成功");
        }
        return JSONResult.error(ErrorCode.WE_CHAT_REPEAT_ERROR);
    }
    private void initCustomer(RegisterDto registerDto, String userPhone,String name) {
        long id = IdUtil.createSnowflake(RConst.Num.INT_1, RConst.Num.INT_1).nextId();
        long state = BitStatesUtils.addState(RConst.Num.BASE_LONG_0, BitStatesUtils.OP_PHONE);
        state = BitStatesUtils.addState(state, BitStatesUtils.OP_INIT_PAY_PASSWORD);
        Customer customer = new Customer()
                .setId(id)
                .setSex(RConst.Num.INT_1)//默认值男
                .setCreateTime(new Date())
                .setPhone(userPhone)
                .setOpenId(registerDto.getOpenid())
                .setBitState(state)//todo
                .setName(name);
        super.save(customer);
    }
    private LoginRo buildLoginRo(RegisterDto registerDto,String name,String userPhone) {
        LoginRo loginRo = new LoginRo();
        loginRo.setPassword("123456");//todo
        loginRo.setType(RConst.Num.INT_2);
        loginRo.setOpenId(registerDto.getOpenid());
        loginRo.setUsername(name);
        loginRo.setPhone(userPhone);
        loginRo.setAdmin(false);
        return loginRo;
    }
}
